package com.usian.service.impl;

import com.usian.mapper.GoodMapper;
import com.usian.pojo.Goods;
import com.usian.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodMapper goodMapper;

    @Override
    public List<Goods> findAll() {
        return goodMapper.findList();
    }

    @Override
    public void add(Goods goods) {
        //默认不启用
        goods.setStatus("2");
        //默认未审核
        goods.setShenhe("3");
        //默认下架
        goods.setShangjia("2");
        goodMapper.insert(goods);
    }

    @Override
    public void delete(Integer id) {
        Goods goods = goodMapper.selectByPrimaryKey(id);
        if (goods.getStatus().equals(2)){
            goods.setShenhe(goods.getShenhe());
            goods.setShangjia(goods.getShangjia());
            goodMapper.deleteByPrimaryKey(id);
        } else {
            return;
        }

    }

    @Override
    public Goods findById(Integer Id) {
        return goodMapper.selectByPrimaryKey(Id);
    }

    @Override
    public void update(Goods goods) {
        Goods goodsOne = goodMapper.selectByPrimaryKey(goods);
        if (goodsOne.getStatus().equals("2")){
            goodsOne.setShenhe(goodsOne.getShenhe());
            goodsOne.setShangjia(goodsOne.getShangjia());
            goodMapper.updateByPrimaryKey(goods);
        } else {
            return;
        }
    }

    //写两个根据不同状态查询的方法
    @Override
    public List<Goods> findList() {
        return goodMapper.findAll();
    }

    @Override
    public void updateshen(Goods goods) {
        goodMapper.updateByPrimaryKey(goods);
    }

    @Override
    public List<Integer> findRelease() {
        return goodMapper.findRelease();
    }

    @Override
    public void shen(Integer id) {
        goodMapper.shen(id);
    }

}
